import java.util.ArrayList;


public class GoldCode {
	ArrayList<String> resGold;
	int[] goldCode;
	
	public GoldCode(){
		int n = 5;
		int N = (int) (Math.pow(2,n) - 1);
	
		//System.out.println("n : "+ n);
		//System.out.println("N : "+ N);
	
		resGold = new ArrayList<String>();
		
		
		Polynome poly3 = new Polynome(n);
		poly3.getPoly().add(1);
		poly3.getPoly().add(1);
		poly3.getPoly().add(0);
		poly3.getPoly().add(0);
		poly3.getPoly().add(0);
		poly3.generateDegres();
		
		poly3.getPoly().clear();
		poly3.getPoly().add(1);
		poly3.getPoly().add(0);
		poly3.getPoly().add(0);
		poly3.getPoly().add(0);
		poly3.getPoly().add(0);
		
		Polynome poly4 = new Polynome(n);
		poly4.getPoly().add(1);	//0
		poly4.getPoly().add(1);	//1
		poly4.getPoly().add(1); //2
		poly4.getPoly().add(0);	//3
		poly4.getPoly().add(1);	//4
		poly4.generateDegres();
		
		poly4.getPoly().clear();
		poly4.getPoly().add(1);
		poly4.getPoly().add(0);
		poly4.getPoly().add(0);
		poly4.getPoly().add(0);
		poly4.getPoly().add(0);
		
		
		String  res3="";
		String  res4="";
		for(int i=0; i<N; i++){
			res3+=poly3.getPoly().get(0);
			res4+=poly4.getPoly().get(0);
			
			poly3.calcOut();
			poly4.calcOut();
	
		}
		//System.out.println("3:\t"+res3+" "+poly3.getSequence());
		//System.out.println("4:\t"+res4+" "+poly4.getSequence());
	
		Polynome poly5 = new Polynome(N);
		Polynome poly6 = new Polynome(N);
		
		for(int i=N-1; i>=0; i--){
			poly5.getPoly().add(poly3.getSequence().get(i));
			poly6.getPoly().add(poly4.getSequence().get(i));
		}
		
		resGold.add(poly5.toString());
		resGold.add(poly6.toString());
		
		for(int i=0; i<N; i++){
			resGold.add(XOR(poly5, poly6, N));
			poly6.shiftBis(poly6.getPoly().get(N-1));
		}
		
		//System.out.println(resGold);
		//System.out.println(resGold.size());
		
		goldCode = new int[resGold.get(0).length()];
	}
	
	public String XOR(Polynome poly1, Polynome poly2, int N){
		String sequenceGoldTemp = "";
		int indice;
		for(int i=N-1; i>=0; i--){
			indice = (poly1.getPoly().get(i) + poly2.getPoly().get(i)) % 2;
			sequenceGoldTemp += indice;
		}
		return sequenceGoldTemp;
	}
	
	public int[] goldCodeRandomly(){
		int lower = 0;
		int higher = resGold.size();

		//chose of OVSH code randomly
		int randomlyCode = (int)(Math.random() * (higher-lower)) + lower;
		for(int i = 0; i<resGold.get(randomlyCode).length(); i++){
			if(resGold.get(randomlyCode).charAt(i) == '1'){
				goldCode[i] = 1;
			}
			else{
				goldCode[i] = 0;
			}
		}
		return goldCode;		
	}
}
